package clientMain;

/**
 * Contains all information for a client
 * @author Andrew Fox
 *
 */

public class clientInfo {

	public StringBuffer username;
	public StringBuffer firstName;
	public StringBuffer lastName;
	public StringBuffer address=new StringBuffer();
	public StringBuffer city;
	public StringBuffer zipcode=new StringBuffer();
	public StringBuffer USState;
	public StringBuffer USStateCode;
	public StringBuffer email;
	public StringBuffer password;
	public StringBuffer creditCardNumber;
	public StringBuffer cvv2;
	public StringBuffer creditCardExpiry;
	public boolean registered=false;
	public StringBuffer userID;


	public clientInfo(StringBuffer username,StringBuffer password){
		this.username=username;
		this.password=password;
		createNewCreditCard();
		createName();
		createAddress();
		this.registered=true;

	}

	public clientInfo(){
		createName();
		createUserNameAndPassword();
		createNewCreditCard();
		createAddress();
	}

	public void createUserNameAndPassword(){
		this.username=this.firstName.append(this.lastName).append(randomDigits(4));
		StringBuffer pwd=new StringBuffer();
		int pwdLength=(int)(Math.random()*13);
		for (int i=0;i<pwdLength;i++){
			pwd=pwd.append((char)((int)(Math.random()*93)+'!'));
		}
		this.password=pwd;
	}

	public long randomDigits(int numDigits){
		long ret=Math.round(Math.random()*Math.pow(10,numDigits));
		while(ret<Math.pow(10, numDigits-1)){
			ret=Math.round(Math.random()*Math.pow(10,numDigits));
		}
		return ret;
	}

	public void createNewCreditCard(){
		String cardNum=Long.toString(randomDigits(15));
		int luhnSum=0;
		for (int i=0;i<cardNum.length();i++){
			if (i%2==1){
				luhnSum+=Integer.parseInt(Character.toString(cardNum.charAt(i)));
			}
			else{
				int num=Integer.parseInt(Character.toString(cardNum.charAt(i)))*2;
				int sum=0;
				while(num > 0)
				{
					sum += num%10;
					num /= 10;	
				}	
				luhnSum+=sum;
			}
		}
		int lastDigit=luhnSum%10;
		if (lastDigit==0)
			lastDigit=10;
		this.creditCardNumber=new StringBuffer(cardNum.concat(Integer.toString(10-lastDigit)));
		this.cvv2=new StringBuffer(Long.toString(randomDigits(3)));

		StringBuffer month=new StringBuffer(Long.toString(Math.round(Math.random()*12)));
		if (month.length()==1)
			month=new StringBuffer("0").append(month);
		String year=Long.toString(Math.round(Math.random()*3)+2012);
		this.creditCardExpiry=month.append(year);


	}

	public void createName(){
		double prob=Math.random();
		if (prob>0.5){
			prob=Math.random();
			for(Double m:clientGenerator.maleFirstNames.keySet()){
				if (prob<m){
					this.firstName=new StringBuffer(clientGenerator.maleFirstNames.get(m));
					break;
				}
			}
		}
		else{
			prob=Math.random();
			for(Double m:clientGenerator.femaleFirstNames.keySet()){
				if (prob<m){
					this.firstName=new StringBuffer(clientGenerator.femaleFirstNames.get(m));
					break;
				}
			}
		}

		prob=Math.random();
		for(Double m:clientGenerator.lastNames.keySet()){
			if (prob<m){
				this.lastName=new StringBuffer(clientGenerator.lastNames.get(m));
				break;
			}
		}
		this.email=(new StringBuffer(this.firstName).append(".").append(this.lastName).append("@").append("fakeMail.com"));
	}

	public void createAddress(){
		double prob=Math.random();
		for(Double m:clientGenerator.streetNames.keySet()){
			if (prob<m){
				this.address=this.address.append((Math.round(Math.random()*999))).append(" ");
				this.address=this.address.append(clientGenerator.streetNames.get(m));
				int suffix=(int)Math.round(Math.random()*5);
				if (suffix==0)
					this.address=this.address.append(" Ave.");
				else if (suffix==1)
					this.address=this.address.append(" Rd.");
				else if (suffix==2)
					this.address=this.address.append(" St.");
				else if (suffix==3)
					this.address=this.address.append(" Dr.");
				else if (suffix==4)
					this.address=this.address.append(" Blvd.");

				break;
			}
		}

		prob=Math.random();
		for(Double m:clientGenerator.cities.keySet()){
			if (prob<m){
				this.city=clientGenerator.cities.get(m);
				this.USState=new StringBuffer((clientGenerator.cityStates.get(this.city.toString())).toString().toUpperCase());
				this.USStateCode=new StringBuffer(Integer.toString(getUSStateCode()));
				break;
			}
		}

		this.zipcode=this.zipcode.append(Math.round(Math.random()*99999));
		for (int i=0;i<(5-this.zipcode.length());i++)
			this.zipcode=new StringBuffer("0").append(this.zipcode);
	}

	public int getUSStateCode(){
		int USStateCode=0;
		String USState=this.USState.toString();
		if (USState.equals("ALABAMA")||USState.equals("AL"))
			USStateCode=1;
		if (USState.equals("ALASKA")||USState.equals("AK"))
			USStateCode=2;
		if (USState.equals("ARIZONA")||USState.equals("AZ"))
			USStateCode=3;
		if (USState.equals("ARKANSAS")||USState.equals("AR"))
			USStateCode=4;
		if (USState.equals("CALIFORNIA")||USState.equals("CA"))
			USStateCode=5;
		if (USState.equals("COLORADO")||USState.equals("CO"))
			USStateCode=6;
		if (USState.equals("CONNECTICUT")||USState.equals("CT"))
			USStateCode=7;
		if (USState.equals("DELAWARE")||USState.equals("DE"))
			USStateCode=8;
		if (USState.equals("FLORIDA")||USState.equals("FL"))
			USStateCode=9;
		if (USState.equals("GEORGIA")||USState.equals("GA"))
			USStateCode=10;
		if (USState.equals("HAWAII")||USState.equals("HI"))
			USStateCode=11;
		if (USState.equals("IDAHO")||USState.equals("ID"))
			USStateCode=12;
		if (USState.equals("ILLINOIS")||USState.equals("IL"))
			USStateCode=13;
		if (USState.equals("INDIANA")||USState.equals("IN"))
			USStateCode=14;
		if (USState.equals("IOWA")||USState.equals("IA"))
			USStateCode=15;
		if (USState.equals("KANSAS")||USState.equals( "KS"))
			USStateCode=16;
		if (USState.equals("KENTUCKY")||USState.equals("KY"))
			USStateCode=17;
		if (USState.equals("LOUISIANA")||USState.equals("LA"))
			USStateCode=18;
		if (USState.equals("MAINE")||USState.equals("ME"))
			USStateCode=19;
		if (USState.equals("MARYLAND")||USState.equals("MD"))
			USStateCode=20;
		if (USState.equals("MASSACHUSETTS")||USState.equals("MA"))
			USStateCode=21;
		if (USState.equals("MICHIGAN")||USState.equals("MI"))
			USStateCode=22;
		if (USState.equals("MINNESOTA")||USState.equals("MN"))
			USStateCode=23;
		if (USState.equals("MISSISSIPPI")||USState.equals("MS"))
			USStateCode=24;
		if (USState.equals("MISSOURI")||USState.equals( "MO"))
			USStateCode=25;
		if (USState.equals("MONTANA")||USState.equals("MT"))
			USStateCode=26;
		if (USState.equals("NEBRASKA")||USState.equals("NE"))
			USStateCode=27;
		if (USState.equals("NEVADA")||USState.equals("NV"))
			USStateCode=28;
		if (USState.equals("NEW HAMPSHIRE")||USState.equals("NH"))
			USStateCode=29;
		if (USState.equals("NEW JERSEY")||USState.equals("NJ"))
			USStateCode=30;
		if (USState.equals("NEW MEXICO")||USState.equals("NM"))
			USStateCode=31;
		if (USState.equals("NEW YORK")||USState.equals("NY"))
			USStateCode=32;
		if (USState.equals("NORTH CAROLINA")||USState.equals("NC"))
			USStateCode=33;
		if (USState.equals("NORTH DAKOTA")||USState.equals("ND"))
			USStateCode=34;
		if (USState.equals("OHIO")||  USState.equals( "OH"))
			USStateCode=35;
		if (USState.equals("OKLAHOMA")||USState.equals("OK"))
			USStateCode=36;
		if (USState.equals("OREGON" )||  USState.equals("OR"))
			USStateCode=37;
		if (USState.equals("PENNSYLVANIA" )||  USState.equals("PA"))
			USStateCode=38;
		if (USState.equals("RHODE ISLAND")||USState.equals("RI"))
			USStateCode=39;
		if (USState.equals("SOUTH CAROLINA" )||  USState.equals("SC"))
			USStateCode=40;
		if (USState.equals("SOUTH DAKOTA")||  USState.equals("SD"))
			USStateCode=41;
		if (USState.equals("TENNESSEE")||  USState.equals("TN"))
			USStateCode=42;
		if (USState.equals("TEXAS")||USState.equals("TX"))
			USStateCode=43;
		if (USState.equals("UTAH")||USState.equals("UT"))
			USStateCode=44;
		if (USState.equals("VERMONT")||USState.equals("VT"))
			USStateCode=45;
		if (USState.equals("VIRGINIA")||USState.equals("VA"))
			USStateCode=46;
		if (USState.equals("WASHINGTON")||  USState.equals("WA"))
			USStateCode=47;
		if (USState.equals("WEST VIRGINIA")||USState.equals("WV"))
			USStateCode=48;
		if (USState.equals("WISCONSIN")||  USState.equals("WI"))
			USStateCode=49;
		if (USState.equals("WYOMING")||USState.equals("WY"))
			USStateCode=50;
		return USStateCode;
	}

}
